
//////////////////////////////////////////////////
// EASIM project
// easim.win.EditControler
// 6/7/2007 6:39 PM
// Cong Liu
// gzcong@gmail.com

// Copyright (C) 2007
// All rights reserved.
// Refer to LICENSE of JIST (Java In Simulation Time) for terms and conditions of use.

package easim.win;

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import java.util.HashSet;

import easim.net.Node;
import easim.net.FieldInterface;

import jist.runtime.JistAPI.DoNotRewrite;

public class EditControler implements Controler, DoNotRewrite
{

  public abstract static class PopupMenu extends JPopupMenu implements DoNotRewrite
  {
    public abstract void setNode(Node node);
  }

  public javax.swing.JPopupMenu popupMenu1;
  public PopupMenu popupMenu2;

	public Object info;

	private FieldPane fieldPane;
	private Node selectedNode;

	public static final int BUTTON1 = 1;
	public static final int BUTTON2 = 2;
	public static final int BUTTON3 = 3;

	private int button;

	public EditControler(FieldPane fieldPane) {
		this.fieldPane = fieldPane;
	}

	public void init() {
		this.selectedNode = null;
	}

	private int dx;
	private int dy;

	public void mousePressed1(int x, int y) {
    if (x <= 0 || y <= 0 || x >= FieldInterface.FIELD_SIZE || y >= FieldInterface.FIELD_SIZE) return;
		this.button = BUTTON1;
    if (this.selectedNode == null) {
      this.selectedNode = fieldPane.nodeFactory.create(0);
      SynControler.addNode(this.selectedNode, x, y);
      this.dx = 0;
      this.dy = 0;
      this.fieldPane.repaint();
    }
    else {
      this.dx = ((int)this.selectedNode.getX()) - x;
      this.dy = ((int)this.selectedNode.getY()) - y;
    }
	}

	public void mousePressed2(int x, int y) {
    if (x <= 0 || y <= 0 || x >= FieldInterface.FIELD_SIZE || y >= FieldInterface.FIELD_SIZE) return;
		this.button = BUTTON2;
    if (this.selectedNode != null) {
      SynControler.deleteNode(this.selectedNode);
      this.selectedNode = null;
      this.fieldPane.repaint();
    }
	}

	public void mousePressed3(int x, int y) {
    if (x <= 0 || y <= 0 || x >= FieldInterface.FIELD_SIZE || y >= FieldInterface.FIELD_SIZE) return;
		this.button = BUTTON3;
		if (selectedNode == null) {
      if (popupMenu1 != null) {
        popupMenu1.show(fieldPane, x, y);
      }
    }
    else {
      if (popupMenu2 == null) return;
      popupMenu2.setNode(selectedNode);
      popupMenu2.show(fieldPane, (int)selectedNode.getX(), (int)selectedNode.getY());
    }
	}

	public void mouseReleased(int x, int y) {
	}

	public void mouseDragged(int x, int y) {
    if (x <= 0 || y <= 0 || x >= FieldInterface.FIELD_SIZE || y >= FieldInterface.FIELD_SIZE) return;
    x += this.dx;
    y += this.dy;
    if (this.button == BUTTON1 && this.selectedNode != null) {
      SynControler.moveNode(this.selectedNode, x, y);
      this.fieldPane.repaint();
    }
	}

	public void mouseMoved(int x, int y) {
    if (x <= 0 || y <= 0 || x >= FieldInterface.FIELD_SIZE || y >= FieldInterface.FIELD_SIZE) return;
    Node newSelectedNode = SynControler.getNode(x, y, Frame.NODE_SIZE);
    if (this.selectedNode != newSelectedNode) {
      this.selectedNode = newSelectedNode;
      this.fieldPane.repaint();
    }
	}

	public synchronized void paint(Graphics graphics) {
		int width = this.fieldPane.getWidth();
		int height = this.fieldPane.getHeight();
    easim.draw.Field field = SynControler.getField(width, height);
    easim.draw.Painter.paint(graphics, field);
		if (this.selectedNode != null) {
      easim.draw.Painter.paint(graphics, this.selectedNode.toString(), Frame.LABEL_SIZE,
        (int)this.selectedNode.getX(), ((int)this.selectedNode.getY()) - Frame.NODE_SIZE - 5, 4);
		}
    if (this.info != null) {
      easim.draw.Painter.paint(graphics, this.info.toString(), Frame.LABEL_SIZE, 4, 4, 2);
    }
	}

}
